package com.ruoyi.college.service.impl;

import java.util.List;

import com.ruoyi.college.domain.*;
import com.ruoyi.college.domain.Class;
import com.ruoyi.college.service.*;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.college.mapper.CourseClassMapper;
import com.ruoyi.common.core.text.Convert;

/**
 * 课程与班级关联Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-26
 */
@Service
public class CourseClassServiceImpl implements ICourseClassService 
{
    @Autowired
    private CourseClassMapper courseClassMapper;

    @Autowired
    private IClassService classService;

    @Autowired
    private ITeacherService teacherService;

    @Autowired
    private ICourseArrangementService  courseArrangementService;

    @Autowired
    private ICourseService courseService;

    @Autowired
    private ISemesterService semesterService;

    /**
     * 查询课程与班级关联
     * 
     * @param id 课程与班级关联主键
     * @return 课程与班级关联
     */
    @Override
    public CourseClass selectCourseClassById(Long id)
    {
        return courseClassMapper.selectCourseClassById(id);
    }

    /**
     * 查询课程与班级关联列表
     * 
     * @param courseClass 课程与班级关联
     * @return 课程与班级关联
     */
    @Override
    public List<CourseClass> selectCourseClassList(CourseClass courseClass)
    {
        List<CourseClass> courseClasses = courseClassMapper.selectCourseClassList(courseClass);
        courseClasses.forEach(item -> {
            Long classId = item.getClassId();
            Class aClass = classService.selectClassByClassId(classId);
            item.setClassName(aClass.getClassName());
            Long teacherId = item.getTeacherId();
            Teacher teacher = teacherService.selectTeacherByTeacherId(teacherId);
            item.setTeacherName(teacher.getTeacherName());
            Long arrangementId = item.getArrangementId();
            CourseArrangement courseArrangement = courseArrangementService.selectCourseArrangementByArrangementId(arrangementId);
            Long courseId = courseArrangement.getCourseId();
            Course course = courseService.selectCourseByCourseId(courseId);
            item.setCourseName(course.getCourseName());
            Long semesterId = courseArrangement.getSemesterId();
            Semester semester = semesterService.selectSemesterBySemesterId(semesterId);
            item.setSemesterId(semesterId);
            item.setSemesterName(semester.getSemesterName());
            Long leaderId = courseArrangement.getLeaderId();
            Teacher leader = teacherService.selectTeacherByTeacherId(leaderId);
            item.setLeaderId(leaderId);
            item.setLeaderName(leader.getTeacherName());
        });
        return courseClasses;
    }

    /**
     * 新增课程与班级关联
     * 
     * @param courseClass 课程与班级关联
     * @return 结果
     */
    @Override
    public int insertCourseClass(CourseClass courseClass)
    {
        courseClass.setCreateTime(DateUtils.getNowDate());
        return courseClassMapper.insertCourseClass(courseClass);
    }

    /**
     * 修改课程与班级关联
     * 
     * @param courseClass 课程与班级关联
     * @return 结果
     */
    @Override
    public int updateCourseClass(CourseClass courseClass)
    {
        return courseClassMapper.updateCourseClass(courseClass);
    }

    /**
     * 批量删除课程与班级关联
     * 
     * @param ids 需要删除的课程与班级关联主键
     * @return 结果
     */
    @Override
    public int deleteCourseClassByIds(String ids)
    {
        return courseClassMapper.deleteCourseClassByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除课程与班级关联信息
     * 
     * @param id 课程与班级关联主键
     * @return 结果
     */
    @Override
    public int deleteCourseClassById(Long id)
    {
        return courseClassMapper.deleteCourseClassById(id);
    }


    @Override
    public List<CourseClass> getClassesByArrangementId(Long arrangementId) {
        List<CourseClass> courseClasses = courseClassMapper.selectCourseClassByArrangementId(arrangementId);
        courseClasses.forEach(item -> {
            Class aClass = classService.selectClassByClassId(item.getClassId());
            item.setClassName(aClass.getClassName());
        });
        return courseClasses;
    }
}
